<?php
/**
 * Created by PhpStorm.
 * User: 王凯林
 * Date: 18/3/27
 * Time: 16:26
 */

namespace App\Http\Controllers\Admin;

use App\Common\Model\ShopAccount;
use App\Common\Model\ShopPayUnline;
use App\Common\Model\ShopUserStatus;
use App\Common\Service\RecommendService;
use App\Common\Service\ShopAccountService;
use App\Common\Tool\AxAes;
use App\Http\Controllers\Controller;
use App\Model\AixinPay;
use App\Model\Btree;
use App\Model\Scores;
use App\Model\Setting;
use App\Service\MemberLevelService;
use App\Service\PeakReportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AccountVerfController extends Controller
{
    protected $recommendService;
    protected $shopAccountService;
    protected $peakReportService;
    protected $memberLevelService;
    protected $beTopParent = [
        1, 2
    ];
    public function __construct(RecommendService $recommendService,ShopAccountService $accountService,
     PeakReportService $peakReportService,MemberLevelService $memberLevelService)
    {
        $this->recommendService = $recommendService;
        $this->shopAccountService = $accountService;
        $this->peakReportService = $peakReportService;
        $this->memberLevelService = $memberLevelService;
    }

    /**
     * 后台审核开卡列表
     */
    public function index(Request $request){
     //从数据表中查询出数据
      $queryStr = $request->querystr;
        $datas = ShopPayUnline::from('shop_pay_unline as s')
            ->where('s.types',0)
            ->where(function ($query) use ($queryStr){
                $query->where('s.num', 'like', $queryStr.'%')
                    ->orWhere('a.name','like',$queryStr.'%');
            })
            ->join('shop_accounts as a',function ($join){
                $join->on('a.uid','=','s.uid');
            })
            ->join('member_levels as l','l.id','=','a.level')
            ->select('s.*','a.name as uname','l.level_money','l.level_name')
            ->orderby('s.create_time','desc')
            ->paginate(20);
      return view('admin.actverf.index',['datas'=>$datas,'querystr'=>$queryStr]);
    }
    /*
     * 后台客服的审核>>>支付表>>>直推者奖励>>>状态表>>>积分表>>>收益额度
     */
    public function updateStatus(Request $request){
        //使用事务来进行操作
        DB::beginTransaction();
        try{
            //操作shop_pay_Unline表
            $rs1 = ShopPayUnline::where('id', $request->id)->update(['status' => $request->status,'update_time'=>time()]);
            //操作aixin_pay表
            $data = [
                'channel'=>1,   //支付渠道为线下‘1’
                'uid'=>$request->uid,
                'money'=>$request->money,
                'status'=>1,    //状态为1，支付完成
                'pay_time'=>$request->paytime,
                'create_time'=>$request->paytime,
            ];
            $rs2 = AixinPay::create($data);
            //直推者奖励
            //$rs3 = $this->recommendService->storeRecommendInfo($request->uid);
            //操作user_status表
            $isOk = $this->shopAccountService->setUserStatus($request->uid, ShopUserStatus::VIP_STATUS, ShopUserStatus::STATUS_SUCCESS);
            //操作积分表
            $mTs = new Setting();
            $proportion = $mTs->getMoneyToScoresSet();
            $bill = $request->money*$proportion;
            $data1 = [
                'uid'=>$request->uid,
                'bill'=>$bill,
                'remark'=>'开卡积分',
                'create_time'=>time()
            ];
            $rs4 = Scores::create($data1);
            //初始化用户收益额度>>>操作peak_report表,status为1表示审核成功
            $total = $this->memberLevelService->compute($request->uid);
            $rs5 = $this->peakReportService->addUserPeak($request->uid,$request->paytime,$total,'开卡初始收益额度',1);
            // 特殊上级或者特殊号码直接成为树顶点
            $shopAcount = ShopAccount::where('uid', $request->uid)->first();
            $pUid = $shopAcount->p_uid;
            $mobile = AxAes::decrypt($shopAcount->phone);
            if (in_array($pUid, $this->beTopParent) || preg_match('/^[18]{2}0{8}\d$/', $mobile)) {
                (new Btree())->addNode($request->uid, 0, '');
            }
            $rs5 = $this->peakReportService->addUserPeak($request->uid,$request->paytime,$total,'开卡初始收益额度',1);
            if ($rs1 && $rs2 && $isOk && $rs4 && $rs5){
                DB::commit();
            }
        }catch(\Exception $e){
            DB::rollBack();
            return ApiResponse([], '修改失败', 1);
        }
        return ApiResponse([]);
    }
}